﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.Common;
using MINERP.MODEL.FoundationModel;
using DevExpress.XtraEditors.Controls;
using MINERP.BLL.Statistics;
using DevExpress.XtraGrid.Views.Grid;
using MINERP.COM;
using MINERP.ComControls.Extension;

namespace MINERP.Statistics.Forms
{
    public partial class FrmNewInnerReturnStat : Form
    {
        private NewInnerReturnBLL _bll;
        private FrmNewInnerReturnStat()
        {
            InitializeComponent();
            _bll = new NewInnerReturnBLL();
            gridView1.ShowRowIndicator(50);
        }

        private static FrmNewInnerReturnStat _instance;

        public static FrmNewInnerReturnStat GetInstance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FrmNewInnerReturnStat();
                }

                return _instance;
            }
        }

        private void FrmNewInnerReturnStat_Load(object sender, EventArgs e)
        {
            dtStart.DateTime = DateTime.Now.AddDays(-1).Date;
            dtEnd.DateTime = DateTime.Now.Date;
            
            //加载人员列表
            cmkRUser.Properties.DataSource = UserBLL.GetAllUsers();
            cmkRUser.Properties.DisplayMember = "UserName";
            cmkRUser.Properties.ValueMember = "UserID";
        }

        List<NewInnerReturn_Info> _source;
        private void btnOK_Click(object sender, EventArgs e)
        {

            this.Cursor = Cursors.WaitCursor;
            //List<NewInnerReturn_Info> result = new List<NewInnerReturn_Info>();
            DateTime start = dtStart.DateTime.Date;
            DateTime end = dtEnd.DateTime.AddDays(1).Date.AddSeconds(-1);
            if (dtStart.DateTime >= dtEnd.DateTime)
            {
                MessageBox.Show("起始日期必须小于结束日期！");
                this.Cursor = Cursors.Default;
                return;
            }

            List<int> SelectedUserIdList = new List<int>();
            foreach (CheckedListBoxItem item in cmkRUser.Properties.Items)
            {
                if (item.CheckState == CheckState.Checked)
                {
                    SelectedUserIdList.Add(Convert.ToInt32(item.Value));
                }
            }

            if (SelectedUserIdList.Count > 0)
            {
                _source = _bll.GetAllInnerReturn(start, end, SelectedUserIdList);
                BindMainView();
            }
            else
            {
                MessageBox.Show("请选择要查看的责任人！");
                this.Cursor = Cursors.Default;
                return;
            }
            this.Cursor = Cursors.Default;
        }

        void BindMainView()
        {
            List<NewInnerReturnStataView_Ifno> listView = new List<NewInnerReturnStataView_Ifno>();
            foreach (var item in _source.GroupBy(i => i.ReturnUserID))
            {
                listView.Add(new NewInnerReturnStataView_Ifno()
                {
                    ReturnUserID = item.Key,
                    ReturnUserName = item.FirstOrDefault().ReturnUserName,
                    TotalAreas = item.Sum(t => t.PhotoArea)

                });
            }

            gridControl1.DataSource = listView;
            gridControl1.MainView = gridView2;
        }

        private void btnReback_Click(object sender, EventArgs e)
        {
            BindMainView();
            btnReback.Enabled = false;
        }

        private void repositoryItemHyperLinkEdit1_Click(object sender, EventArgs e)
        {
            btnReback.Enabled = true;
            int returnUserId = ((NewInnerReturnStataView_Ifno)gridView2.GetFocusedRow()).ReturnUserID;
            gridControl1.DataSource = _source.Where(t => t.ReturnUserID == returnUserId).ToList();
            gridControl1.MainView = gridView1;
        }

        private void FrmNewInnerReturnStat_FormClosing(object sender, FormClosingEventArgs e)
        {
            _instance = null;
        }

        private void simpleButtonExport_Click(object sender, EventArgs e)
        {
            //var gridview = gridControl1.MainView;
            //if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            //{
            //    //saveFileDialog1.FileName = cmbGroup.Text+"组的员工业绩.xls";

            //    string saveDir = saveFileDialog1.FileName;
            //    if (!string.IsNullOrEmpty(saveDir))
            //    {

            //        gridControl1.MainView.ExportToXls(saveDir);
            //        MessageBox.Show("导出成功!");
            //    }
            //}
            //DevGridViewExport.ExportToExcel("内部返单统计"+DateTime.Now.ToString("yyyyMMddHHmmss"), gridView1);
            gridControl1.ExportToExcel(this, "内部返单统计" + DateTime.Now.ToString("yyyyMMddHHmmss"));
        }


    }
}
